package org.example.dao;

import org.example.entity.Department;
import org.example.utils.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;


public class DepartmentDAO {
    //部门新增
    public static boolean insertDepartment(Department department) {
        try (Connection connection = JdbcUtils.getConnection()) {
            String sql = "INSERT INTO ims_department (dpt_id,dpt_name,create_time,update_time) VALUES (?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, department.getDptId());
            preparedStatement.setString(2, department.getDptName());
            preparedStatement.setObject(3, new java.sql.Timestamp(new java.util.Date().getTime()));
            preparedStatement.setObject(4, new java.sql.Timestamp(new java.util.Date().getTime()));

            int i = preparedStatement.executeUpdate();
            return i != 0;
        } catch (SQLException e) {

            System.out.println("数据库操作失败！请联系技术解决："+e.getMessage());
            return false;
        }
    }

    //检查部门名称是否存在
    public static boolean queryDepartmentByName(String departmentName) {
        try (Connection connection = JdbcUtils.getConnection()) {
            String sql = "SELECT dpt_name FROM ims_department WHERE dpt_name = ? AND is_deleted = 0";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, departmentName);

            ResultSet resultSet = preparedStatement.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            System.out.println("出错！请稍后再试："+e.getMessage());
            return false;
        }
    }

    //查询所有部门信息
    public static ArrayList<String[]> getDepartmentInfo() {
        try (Connection connection = JdbcUtils.getConnection()) {
            String sql = "SELECT dpt_id,dpt_name FROM ims_department WHERE is_deleted = 0";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            ArrayList<String[]> resData = new ArrayList<>();

            while (resultSet.next()){
                String[] dataColumnList = new String[2];
                dataColumnList[0] = resultSet.getString("dpt_id");
                dataColumnList[1] = resultSet.getString("dpt_name");
                resData.add(dataColumnList);
            }
            return resData;
        } catch (SQLException e) {
            System.out.println("出错！请稍后再试："+e.getMessage());
            return null;
        }
    }

    public static boolean updateDepartment(String id, String newDepartName) {
        //部门名称校验
        if (checkDepartName(newDepartName)){
            System.out.println("不能使用和其他部门相同的名称");
            return false;
        }
        try (Connection connection = JdbcUtils.getConnection()) {
            String sql = "UPDATE ims_department SET dpt_name = ?,update_time = ? WHERE is_deleted = 0 AND dpt_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, newDepartName);
            preparedStatement.setObject(2, new java.sql.Timestamp(new java.util.Date().getTime()));
            preparedStatement.setString(3, id);

            int i = preparedStatement.executeUpdate();
            return i == 1;
        } catch (SQLException e) {
            System.out.println(e);
            System.out.println("数据库操作失败！请联系技术解决："+e.getMessage());
            return false;
        }
    }

    public static boolean checkDepartName(String departName) {
        try (Connection connection = JdbcUtils.getConnection()) {
            String sql = "SELECT dpt_name FROM ims_department WHERE is_deleted = 0 AND dpt_name = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, departName);
            ResultSet res = preparedStatement.executeQuery();
            return res.next();
        } catch (SQLException e) {
            System.out.println("数据库操作失败！请联系技术解决："+e.getMessage());
            return false;
        }
    }

    public static boolean deleteDepartment(String id) {
        try (Connection connection = JdbcUtils.getConnection()) {
            String sql = "UPDATE ims_department SET is_deleted = 1 WHERE dpt_id = ?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);

            int i = preparedStatement.executeUpdate();
            return i == 1;
        } catch (SQLException e) {
            System.out.println("数据库操作失败！请联系技术解决："+e.getMessage());
            return false;
        }
    }
}
